<template>
  <div>
    <el-container style="background: #f5f7f9;">
      <el-header style="background: #fff;padding: 12px 12px;height:auto;">
        <div>
          <el-form :model="queryData" :inline="true" size="small">
            <el-form-item label="批次号">
              <el-input v-model="queryData.name" :rows="1" style="width: 280px;" type="textarea" placeholder="多个单号用回车分割" />
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="searchData()">搜索</el-button>
            </el-form-item>
          </el-form>
        </div>
      </el-header>
      <el-main style="margin-top: 15px; background: #fff;padding: 12px 16px 0 16px;height: 100%;">
        <div style="height: 100%;display: flex;">
          <el-button size="small" @click="add">新增换单批次</el-button>
        </div>
        <el-table ref="multipleTable" :data="tableData" stripe tooltip-effect="dark" :max-height="tableHeight" border>
          <el-table-column prop="name" fixed label="批次号" />
          <el-table-column prop="totalCarton" label="总件数" />
          <el-table-column prop="totalCartonPrepared" label="有面单件数" />
          <el-table-column prop="totalCartonReplaced" label="已换单件数" />
          <el-table-column prop="createdAt" label="创建时间" />
          <el-table-column label="操作" fixed="right">
            <template slot-scope="scope">
              <el-button size="small" shape="circle" type="text" @click="importTrackingNumbers(scope.row)">导入转单号</el-button>
              <el-button size="small" shape="circle" type="text" @click="showLabelDialog(scope.row)">导入面单</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-main>
      <el-footer style="padding:0;background: #fff;">
        <div style="text-align: right; padding-top: 10px;">
          <!-- 分页 -->
          <el-pagination background :current-page="queryData.currentPage" :page-sizes="[50, 100, 200, 300, 400]" :page-size="queryData.pageSize" layout="total, sizes, prev, pager, next, jumper" :total="total" style="float: right;" @size-change="handleSizeChange" @current-change="handleCurrentChange" />
        </div>
      </el-footer>
    </el-container>
    <el-dialog :visible.sync="openModal" width="30%">
      <el-form :model="formData">
        <el-row :gutter="32">
          <el-col :span="24">
            <el-form-item label="批次号">
              <el-input v-model="formData.name" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer">
        <el-button style="margin-right: 8px" @click="openModal = false">取消</el-button>
        <el-button type="primary" @click="submit">确定</el-button>
      </div>
    </el-dialog>

    <el-dialog title="导入转单号" :visible.sync="importTrackingNumbersDialog" width="30%">
      <el-upload
        :action="importTrackingNumberUrl"
        :on-success="importCallBack"
        :data="trackingNumberForm"
        name="file"
        :limit="1"
        :file-list="importFileList"
        accept=".xlsx"
        :with-credentials="true"
        :show-file-list="false"
        style="display: inline-block;"
      >
        <el-button slot="trigger" size="small" type="primary">选取文件</el-button>
      </el-upload>
      <a href="/template/replacelabel.xlsx" download="换单.xlsx" style="margin-left:50px;display: inline-block; vertical-align: top;">
        <el-button size="small" type="text">下载模板</el-button>
      </a>
    </el-dialog>

    <el-dialog title="上传面单" :visible.sync="importLabelDialog" width="30%">
      <el-upload
        :action="labelForm.host"
        :on-success="labelCallBack"
        :limit="50"
        :multiple="true"
        :data="labelForm"
        :before-upload="beforeLabelUpload"
        :file-list="importFileList"
        :on-exceed="handleExceed"
        accept=".pdf"
        style="display: inline-block;"
      >
        <el-button slot="trigger" size="small" type="primary">选取文件</el-button>
      </el-upload>
    </el-dialog>
  </div>
</template>

<script>
import request from '@/utils/request'
import config from '@/settings'
import { copyProperty } from '@/utils/tools'
const BASE_URL = process.env.NODE_ENV === 'development' ? config.baseUrl.dev : config.baseUrl.pro
export default {
  data() {
  return {
      openModal: false,
      importTrackingNumbersDialog: false,
      importLabelDialog: false,
      formData: {
        name: ''
      },
      trackingNumberForm: {
        id: ''
      },
      labelForm: {
        key: '',
        OSSAccessKeyId: '',
        policy: '',
        signature: '',
        dir: '',
        host: '',
        expire: '',
        success_action_status: '200'
      },
      importFileList: [],
      importTrackingNumberUrl: BASE_URL + '/replacelabel/tracking-number',
      queryData: {
        pageSize: 50,
        currentPage: 0,
        name: ''
      },
      tableData: [],
      currentPage: 1, // 分页
      total: 0,
      labelIds: []
    }
  },
  computed: {
    tableHeight() {
      return document.documentElement.clientHeight - 250
    }
  },
  created() {
    this.queryOrder()
  },
  methods: {
    handleExceed(files, fileList) {
      this.$message({
        message: '一次最多只能上传30个PDF文件',
        type: 'warning',
        duration: 1000
      })
    },
    showLabelDialog(row) {
      if (row.totalCarton === 0) {
        this.$message.error('请先导入转单号')
        return
      }
      this.labelIds = []
      this.trackingNumberForm.id = row.id
      this.importLabelDialog = true
    },
    beforeLabelUpload() {
      const _this = this
      return new Promise((resolve, reject) => {
        request({
            url: 'oss/policy',
            method: 'get'
          }).then(res => {
            if (res.success) {
              copyProperty(_this.labelForm, res.data)
              _this.labelIds.push(res.data.key)
              resolve(true)
            }
          })
      })
    },
    importTrackingNumbers(row) {
      this.trackingNumberForm.id = row.id
      this.importTrackingNumbersDialog = true
    },
    submit() {
      request({
        url: 'replacelabel',
        data: this.formData,
        method: 'post'
      }).then(res => {
        if (res.success) {
          this.openModal = false
          this.queryOrder()
        }
      })
    },
    add() {
      this.formData.name = ''
      this.openModal = true
    },
    labelCallBack() {
      this.importLabelDialog = false
      this.importFileList = []
      request({
        url: 'replacelabel/label',
        method: 'post',
        data: { id: this.trackingNumberForm.id, labelOssKeys: this.labelIds }
      }).then(res => {
      })
    },
    importCallBack(res) {
      if (res.success) {
        this.$message.success('成功')
        this.importTrackingNumbersDialog = false
        this.importFileList = []
        this.queryOrder()
      } else {
        this.importFileList = []
        this.$message({
          message: res.message,
          type: 'error',
          duration: 0,
          showClose: true
        })
      }
    },
    searchData() {
      this.queryData.currentPage = 1
      this.queryOrder()
    },
    // 搜索
    queryOrder() {
      request({
        url: 'replacelabel/query',
        data: this.queryData,
        method: 'post'
      }).then(res => {
        if (res.success) {
          this.tableData = res.data.records
          this.total = res.data.total
        }
      })
    },
    // 删除
    handleDelete(index, row) {
      this.$confirm('是否取消的订单?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {

      })
    },
    downloadLabel(row) {
      request({
        url: '/d/lm-label?orderId=' + row.orderId,
        method: 'get'
      }).then(res => {
        if (res.success) {
            window.open(res.data)
        }
      })
    },
    // 分页
    handleSizeChange(val) {
      this.queryData.currentPage = 1
      this.queryData.pageSize = val
      this.queryOrder()
    },
    handleCurrentChange(val) {
      this.queryData.currentPage = val
      this.queryOrder()
    }
  }
}
</script>
